<?php

/**
 * Copyright (C), 2021-2021, Shall Buy Life info. Co., Ltd.
 * FileName: MicroService.php
 * Description: 调用其他服务 api 服务层
 *
 * @author lwl
 * @Create Date    2021/9/1 13:04
 * @Update Date    2021/9/1 13:04 By lwl
 * @version v1.0
 */

namespace Example\Services;

use Example\Constants\MicroConstant;
use Example\Constants\MicroUriConstant;

class MicroService implements MicroConstant
{
    /**
     * FunctionName：getMemberByMemberIds
     * Description：通过 memberIds 获取 member 数据
     * Author：lwl
     * @param array $memberIds
     * @param bool $arrayColumn
     * @param string $field
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getMemberByMemberIds(array $memberIds, bool $arrayColumn = true, string $field = 'id'): array
    {
        $result = \Api::post(MicroUriConstant::GET_MEMBER_BY_MEMBER_IDS)->json(['ids' => $memberIds])->run()->getData();
        if (!empty($result['list']) && $arrayColumn == true && $field != '') {
            return array_column($result['list'], null, $field);
        }
        return $result['list'];


    }

    /**
     * FunctionName：getMemberHeadPicByMemberIds
     * Description：通过 memberIds 获取 会员 头像
     * Author：lwl
     * @param array $memberIds
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getMemberHeadPicByMemberIds(array $memberIds): array
    {
        return \Api::post(MicroUriConstant::GET_MEMBER_HEAD_PIC_BY_MEMBER_IDS)->json(['ids' => $memberIds])->run()->getData();
    }

    /**
     * FunctionName：getRoleByRoleId
     * Description：通过roleId 获取 角色详情
     * Author：lwl
     * @param int $roleId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getRoleByRoleId(int $roleId): array
    {
        $result = \Api::get(MicroUriConstant::GET_ROLE_BY_ROLE_ID)->json(['id' => $roleId])->run()->getData();
        return count($result['list']) > 0 ? reset($result['list']) : [];
    }

    /**
     * FunctionName：getStaffByStaffId
     * Description：通过 staffId 获取 staff 信息
     * Author：lwl
     * @param int $staffId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStaffByStaffId(int $staffId): array
    {
        return \Api::get(MicroUriConstant::GET_STAFF_BY_STAFF_ID)->json(['id' => $staffId])->run()->getData();
    }

    /**
     * FunctionName：getStoreByMemberId
     * Description：通过 memberId 获取 商户信息
     * Author：lwl
     * @param int $memberId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStoreByMemberId(int $memberId): array
    {
        return \Api::post(MicroUriConstant::GET_STORE_BY_MEMBER_ID)->json(['member_id' => $memberId])->run()->getData();
    }

    /**
     * FunctionName：getBusinessRoleList
     * Description：获取业务与对应的角色
     * Author：lwl
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getBusinessRoleList(): array
    {
        $result = \Api::get(MicroUriConstant::GET_BUSINESS_ROLE_LIST)->run()->getData();
        return $result['list'] ?? [];
    }

    /**
     * FunctionName：getAdSpaceByAdId
     * Description：通过 广告id 与 label 获取广告详情
     * Author：lwl
     * @param $adId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getAdSpaceByAdId($adId): array
    {
        return \Api::get(MicroUriConstant::GET_AD_BY_AD_ID)->json(['id' => $adId, 'label' => config('school.adspace.label')])->run()->getData();
    }

    /**
     * FunctionName：saveAd
     * Description：向广告服务提交广告数据
     * Author：lwl
     * @param array $params
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function saveAd(array $params): array
    {
        $response = \Api::post(MicroUriConstant::SAVE_SCHOOL_AD)->json(['param' => $params])->run()->getContents();
        $result = json_decode($response, true);
        if ($result['code'] != 0) {
            throw \ExceptionFactory::business(['code' => $result['code'], 'message' => $result['message'] . '无法投放广告，但课程可正常发布']);
        }
        return [];
    }

    /**
     * FunctionName：getStoreByUserName
     * Description：通过 userName 查找 商户
     * Author：lwl
     * @param string $userName
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStoreByUserName(string $userName): array
    {
        return \Api::get(MicroUriConstant::GET_STORE_BY_USER_NAME)->json(['account_name' => $userName])->run()->getData();
    }

    /**
     * FunctionName：getRealNameByMemberId
     * Description：通过 memberId 查询 真实姓名 realName
     * Author：lwl
     * @param int $memberId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getRealNameByMemberId(int $memberId): array
    {
        $result = \Api::post(MicroUriConstant::GET_REAL_NAME_BY_MEMBER_ID)->json(['member_id' => $memberId])->run()->getData();
    }


    /**
     * FunctionName：getStoreUserByUserId
     * Description：通过 userId 获取 store服务的user信息
     * Author：lwl
     * @param int $userId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStoreUserByUserId(int $userId): array
    {
        return \Api::get(MicroUriConstant::GET_STORE_USER_BY_USER_ID)->json(['id' => $userId])->run()->getData();
    }

    /**
     * FunctionName：getStoreByStoreId
     * Description：通过storeId 查询 查询商户
     * Author：lwl
     * @param int $storeId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStoreByStoreId(int $storeId): array
    {
        return \Api::get(MicroUriConstant::GET_STORE_BY_STORE_ID)->json(['id' => $storeId])->run()->getData();
    }

    /**
     * FunctionName：getMemberByPhone
     * Description：通过 phone 获取 会员信息
     * Author：lwl
     * @param string $phone
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getMemberByPhone(string $phone): array
    {
        return \Api::post(MicroUriConstant::GET_MEMBER_BY_PHONE)->json(['phone' => $phone])->run()->getData();
    }

    /**
     * FunctionName：getStoreListByStoreIds
     * Description：通过 storeIds 获取 storeList
     * Author：lwl
     * @param array $storeIds
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getStoreListByStoreIds(array $storeIds): array
    {
        $result = \Api::post(MicroUriConstant::GET_STORE_LIST_BY_STORE_IDS)->json(['ids' => $storeIds])->run()->getData();
        return $result['list'];
    }


    /**
     * FunctionName：getStaffRolesByStaffId
     * Description：通过staffId 获取 员工 角色
     * Author：lwl
     * @param int $staffId
     * @param string $field
     * @return array
     */
    public static function getStaffRolesByStaffId(int $staffId, string $field = '*'): array
    {
        $result = \Api::get(\Decoration\Constants\MicroUriConstant::GET_STAFF_ROLES_BY_STAFF_ID)->json(['id' => $staffId])->run()->getData();
        $collect = collect($result['list']);
        return $field == '*' ? $result['list'] : $collect->pluck($field)->toArray();
    }

    /**
     * FunctionName：getMemberByPhone
     * Description：通过 phones 获取 会员信息
     * Author：zmy
     * @param string $phone
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function getMemberByPhones(array $phones): array
    {
        $result = \Api::post(MicroUriConstant::GET_MEMBER_BY_PHONES)->json(['phones' => $phones])->run()->getData();
        return $result['list'];
    }

    /**
     * FunctionName：getMemberDataByPhones
     * Description：phones 获取 会员信息及实名信息
     * @param array $phones
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2022-02-18 5:13 下午
     */
    public static function getMemberDataByPhones(array $phones): array
    {
        $result = \Api::post(MicroUriConstant::GET_MEMBER_INFO_BY_PHONES)->json(['phones' => $phones])->run()->getData();
        return $result['list'];
    }

    /**
     * FunctionName：createContract
     * Description：创建合同
     * @param array $params
     * @return array
     *
     * @author mdx
     * @date 2021-11-29 7:13 下午
     */
    public static function createContract(array $params): array
    {
        return \Api::post(MicroUriConstant::CREATE_CONTRACT)->json($params)->run()->getData();
    }

    /**
     * FunctionName：getContractSignDetail
     * Description：根据模板获取签章信息
     * @param array $params
     * @return array
     *
     * @author mdx
     * @date 2021-11-30 9:44 上午
     */
    public static function getContractSignDetail(array $params): array
    {
        return \Api::post(MicroUriConstant::GET_CONTRACT_SIGN_DETAIL)->json($params)->run()->getData();
    }

    /**
     * FunctionName：contractSign
     * Description：合同签署
     * @param array $params
     * @return array
     *
     * @author mdx
     * @date 2021-12-01 10:57 上午
     */
    public static function contractSign(array $params): array
    {
        return \Api::post(MicroUriConstant::CONTRACT_SIGN)->json($params)->run()->getData();
    }

    /**
     * FunctionName：getMemberAuthByIds
     * Description：获取会员实名信息
     * @param array $params
     * @return array
     *
     * @author mdx
     * @date 2021-12-01 10:57 上午
     */
    public static function getMemberAuthByIds(array $memberIds): array
    {
        $result = \Api::get(MicroUriConstant::GET_MEMBER_AUTH_BY_MEMBER_IDS)->json(['ids'=>$memberIds])->run()->getData();
        return $result['list'] ?? [];
    }

    /**
     * FunctionName：getContractTemplateById
     * Description：获取合同模板数据
     * @param int $tplId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-08 4:51 下午
     */
    public static function getContractTemplateById(int $tplId): array
    {
        return \Api::post(MicroUriConstant::GET_CONTRACT_TEMPLATE_BY_ID)->json(['tpl_id' => $tplId])->run()->getData();
    }

    /**
     * FunctionName：getContractInfo
     * Description：获取合同详情
     * @param int $contractId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-08 4:55 下午
     */
    public static function getContractInfo(int $contractId): array
    {
        return \Api::post(MicroUriConstant::GET_CONTRACT_INFO)->json(['contract_id' => $contractId])->run()->getData();
    }

    /**
     * FunctionName：getMemberDetail
     * Description：获取会员详情
     * @param int $contractId
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-08 4:55 下午
     */
    public static function getMemberDetail(int $memberId): array
    {
        return \Api::get(MicroUriConstant::GET_MEMBER_BY_ID)->json(['id' => $memberId])->run()->getData();
    }

    /**
     * FunctionName：smsCheckCode
     * Description：短信验证
     * @param $params
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-10 1:14 下午
     */
    public static function smsCheckCode(array $params): array
    {
        return \Api::post(MicroUriConstant::SMS_CHECK_CODE)->json($params)->run()->getData();
    }

    /**
     * FunctionName：getSeal
     * Description：获取签章信息
     * @param array $params
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-10 4:38 下午
     */
    public static function getSeal(array $params): array
    {
        return \Api::post(MicroUriConstant::GET_SEAL)->json($params)->run()->getData();
    }

    /**
     * FunctionName：cancelContract
     * Description：撤回合同
     * @param array $params
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     *
     * @author mdx
     * @date 2021-12-14 2:19 下午
     */
    public static function cancelContract(array $params): array
    {
        return \Api::post(MicroUriConstant::CANCEL_CONTRACT)->json($params)->run()->getData();
    }


    /**
     * FunctionName：storeXiaoBaBalance
     * Description：增加小巴余额
     * Author：lwl
     * @param array $params
     * @return array
     * @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
     */
    public static function addXiaoBaBalance(array $params):array
    {
        return \Api::post(MicroUriConstant::STORE_XIAO_BA_BALANCE)->json($params)->run()->getData();
    }

    /**
     * FunctionName: addBusiness
     * Description: 添加业务
     * Author: lhx
     * @param string $businessName 业务名称
     * @param string $businessAlias 业务别名
     * @return array
     */
    public static function addBusiness(string $businessName, string $businessAlias): array
    {
        // TODO: Implement addBusiness() method.
        $params = [
            'business_name' => $businessName,
            'business_alias' => $businessAlias,
        ];
        return \Api::post(MicroUriConstant::STORE_BUSINESS)->json($params)->run()->getData();
    }

    /**
     * FunctionName: getBusinessInfo
     * Description: 获取业务信息
     * Author: lhx
     * @param int $type 查询业务类型
     * @param string $value 查询值
     * @return array
     */
    public static function getBusinessInfo(string $value, int $type = 2): array
    {
        // TODO: Implement getBusinessInfo() method.
        $params = [
            'type' => $type,
            'value' => $value,
        ];
        return \Api::get(MicroUriConstant::GET_BUSINESS_INFO)->json($params)->run()->getData();
    }

    /**
     * FunctionName: addRole
     * Description: 添加角色
     * Author: lhx
     * @param int $businessId 业务ID
     * @param string $roleName 角色名称
     * @param int $roleType 角色类型
     * @return array
     */
    public static function addRole(int $businessId, string $roleName, int $roleType = 1): array
    {
        // TODO: Implement addRole() method.
        $params = [
            'business_id' => $businessId,
            'role_type' => $roleType,
            'role_name' => $roleName,
        ];
        return \Api::post(MicroUriConstant::STORE_MEMBER_ROLE)->json($params)->run()->getData();
    }

    /**
     * FunctionName: bitchAddRole
     * Description: 批量添加角色
     * Author: lhx
     * @param $params
     * @return array
     */
    public static function bitchAddRole($params): array
    {
        return \Api::post(MicroUriConstant::BATCH_STORE_MEMBER_ROLE)->json(['adds' => $params])->run()->getData();
    }

    /**
     * FunctionName: getRoleInfoByRoleName
     * Description:
     * Author: lhx
     * @param string $roleName 角色名称
     * @param int $businessId 业务ID
     * @param int $roleType 角色类型
     * @return array
     */
    public static function getRoleInfoByRoleName(string $roleName, int $businessId, int $roleType = 1): array
    {
        // TODO: Implement getRoleInfo() method.
        $params = [
            'role_name' => $roleName,
            'role_type' => $roleType,
            'business_id' => $businessId
        ];
        return \Api::get(MicroUriConstant::GET_ROLE_INFO_BY_ROLE_NAME)->json($params)->run()->getData();
    }

    /**
     * FunctionName: addMemberAssociation
     * Description: 添加会员角色关系
     * Author: lhx
     * @param int $memberId 会员ID
     * @param int $businessId 业务ID
     * @param int $roleId 角色ID
     * @param int $roleType 角色类型
     * @param int $status 状态
     * @return array
     */
    public static function addMemberAssociation(int $memberId, int $businessId, int $roleId, int $roleType = 1, int $status = 1): array
    {
        // TODO: Implement addMemberAssociation() method.
        $params = [
            'member_id' => $memberId,
            'business_id' => $businessId,
            'role_type' => $roleType,
            'role_id' => $roleId,
            'status' => $status,
        ];
        return \Api::post(MicroUriConstant::STORE_MEMBER_ASSOCIATION)->json($params)->run()->getData();
    }

    /**
     * FunctionName: batchAddMemberAssociation
     * Description: 批量添加会员关系
     * Author: lhx
     * @param array $params 数据
     * @return array
     */
    public static function batchAddMemberAssociation(array $params): array
    {
        // TODO: Implement batchAddMemberAssociation() method.
        return \Api::post(MicroUriConstant::BATCH_STORE_MEMBER_ASSOCIATION)->json(['adds' => $params])->run()->getData();
    }

    /**
     * FunctionName: getMemberAssociation
     * Description: 查询会员关系
     * Author: lhx
     * @param int $memberId
     * @param int $businessId
     * @param int $roleId
     * @param int $roleType
     * @param int $type
     * @return array
     */
    public static function getMemberAssociation(int $memberId, int $businessId, int $roleId, int $roleType = 1, int $type = 1): array
    {
        // TODO: Implement getMemberAssociation() method.
        $params = [
            'member_id' => $memberId,
            'business_id' => $businessId,
            'role_type' => $roleType,
            'role_id' => $roleId,
            'type' => $type,
        ];
        return \Api::get(MicroUriConstant::GET_MEMBER_ASSOCIATION_BY_INFO)->json($params)->run()->getData();
    }

    /**
     * FunctionName: updateMemberAssociationById
     * Description: 修改会员关系
     * Author: lhx
     * @param int $id 主键ID
     * @param int $memberId 会员ID
     * @param int $businessId 业务ID
     * @param int $roleId 角色ID
     * @param int $roleType 角色类型
     * @param int $status 状态
     * @return array
     */
    public static function updateMemberAssociationById(int $id, int $memberId, int $businessId, int $roleId, int $roleType, int $status): array
    {
        // TODO: Implement updateMemberAssociation() method.
        $params = [
            'id' => $id,
            'update' => [
                'member_id' => $memberId,
                'business_id' => $businessId,
                'role_type' => $roleType,
                'role_id' => $roleId,
                'status' => $status,
            ],
        ];
        return \Api::post(MicroUriConstant::UPDATE_MEMBER_ASSOCIATION)->json($params)->run()->getData();
    }
}
